package xyz.lwm.lazycat.utility;

import xyz.lwm.lazycat.utility.exception.UtilException;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;

/**
 * simple date util
 *
 * @author lwm
 */
public abstract class DateUtil {

    public static final String DEFAULT_PATTERN = "yyyy-MM-dd HH:mm:ss.SSS";

    public static final String DATE_TIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static final String DATE_PATTERN = "yyyy-MM-dd";

    public static final String TIME_PATTERN = "HH:mm:ss";

    public static final String DATE_TIME_PATTERN_CHINESE = "yyyy年MM月dd日 HH时mm分ss秒";

    public static final String DATE_PATTERN_CHINESE = "yyyy年MM月dd日";

    public static final String TIME_PATTERN_CHINESE = "HH时mm分ss秒";

    public static Date parse(String str, String pattern) {
        try {
            return new SimpleDateFormat(pattern).parse(str);
        } catch (ParseException e) {
            throw new UtilException(e);
        }
    }

    public static String format(Date date, String pattern) {
        return new SimpleDateFormat(pattern).format(date);
    }

    public static LocalDate parseLocalDate(String str, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).parse(str).query(LocalDate::from);
    }

    public static String formatLocalDate(LocalDate localDate, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(localDate);
    }

    public static LocalDateTime parseLocalDateTime(String str, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).parse(str).query(LocalDateTime::from);
    }

    public static String formatLocalDateTime(LocalDateTime localDateTime, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(localDateTime);
    }

}
